<?php 
/*****************************************************************
Created : 02/02/2011
Author : Mr. Nawin.I
*****************************************************************/
define('ADODB_ASSOC_CASE', 2); 
$rel_path="./";

include($rel_path."adodb/adodb.inc.php");
include($rel_path."adodb/tohtml.inc.php");
include($rel_path."adodb/adodb-pager.inc.php");

$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;

class DB2 {
		var $Connect;
		var $ResultSet;
		var $AffectedRows;
		var $NumRows;
		var $db;
		private $CurrTerm;
		private $CurrYear;
	// constructor connect handle
	function DB2(){
		 
		 $Host = "localhost";
		 $Username = "root";
		 $Password = "sa";
		 $DbName = "shms1";
	
		$DBType = "mysqli";

		$this->db = NewADOConnection($DBType);
		$this->db->Connect($Host, $Username, $Password, $DbName);
		//Fix thailanguage
		$this->db->Execute("SET NAMES utf8");
		
		$this->db->Execute("SET character_set_results=utf8");
		$this->db->Execute("SET character_set_client=utf8");
		$this->db->Execute("SET character_set_connection=utf8");
		
		$this->setCurrentTerm();
		$this->setCurrentYear();
	}
	
	function getPaymentTypeService(){
		$sql = " SELECT id , name
			 FROM tb_type_payment_service
			 WHERE STATUS='Y'
			 ORDER BY name DESC ";
			 
		$result=$this->ExecuteData($sql);
		while($row = $result->FetchRow()){
			$ar[$row["id"]] = $row["name"];
		}
		return $ar;	
	}
	function getTerm(){
		$sql = " SELECT id , name
			 FROM tb_term
			 ORDER BY id asc ";
			 
		$result=$this->ExecuteData($sql);
		while($row = $result->FetchRow()){
			$ar[$row["id"]] = $row["name"];
		}
		return $ar;	
	}
	function getCurrentTerm(){
		return $this->CurrTerm;
	}
	function getYear_Term(){
		$sql = "select date_format( now() ,'%Y') + 544 as year_term
				union 
				select date_format( now() ,'%Y') + 543 as year_term
				union 
				select  year
				from tb_student_year
				group by year ";
		$result=$this->ExecuteData($sql);
		while($row = $result->FetchRow()){
			$ar[$row["year_term"]] = $row["year_term"];
		}
		return $ar;	
	}
	function setCurrentTerm(){
		$sql=  " SELECT value FROM tb_system_config WHERE name='CURR_TERM' ";
		$result=$this->ExecuteData($sql);
		$row = $result->FetchRow();
		$this->CurrTerm = $row['value'];
	}
	function getCurrentYear(){
		return $this->CurrYear;
	}
	function setCurrentYear(){
		$sql=  " SELECT value FROM tb_system_config WHERE name='CURR_YEAR' ";
		$result=$this->ExecuteData($sql);
		$row = $result->FetchRow();
		$this->CurrYear = $row['value'] +543;
	}

	function getDB() {
		return $this->db;
	}
	
	function ExecuteData($sqlScript){
		return $this->db->Execute($sqlScript);
	}
	function ExecuteDataAndGetID($sqlScript){
		$this->db->Execute($sqlScript);
		return $this->db->Insert_ID();
	}	
	function getLastID(){
		return $this->db->Insert_ID();
	}
	
	function InsertSqlScript($sqlScript,$val){
		$rs=$this->ExecuteData($sqlScript . " and 1=0 ");
		return $this->db->GetInsertSQL($rs,$val);
	}
	
	function UpdateSqlScript($sqlScript,$val){
		$rs=$this->ExecuteData($sqlScript);
		return $this->db->GetUpdateSQL($rs,$val);
	}
	
	function GetPager($sql,$list_page) {
		$pager = new ADODB_Pager($this->db,$sql);
		return $pager;
	}
}
?>